﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CmdArgs.Settings;
using CmdArgs.Validation.Options;
using CmdArgs;

namespace QuickStartExample
{
    class Program
    {
        static void Main(string[] args)
        {
            args = new[] { "--help", "--numbers", "9", "6" };
            var settingsBuilder = new SettingsBuilder();

            settingsBuilder.AddFlag("help", 'h')
                .SetDescription("Prints help.")
                .AddLongNameAlias("pomoc");

            settingsBuilder.AddOption("numbers", 'n', OptionParametersCount.AtLeast(1))
                .AsInt(3, 10)    //<- alias za AddValidator(new IntValidator(3,10))
                .AddShortNameAlias('i')
                .SetParameterName("NUMBER");    
            // SetParameterName zpusobi, ze generovana napoveda nebude vypadat -n [PARAM, ...]
            // ale -n [NUMBER, ...]

            settingsBuilder.AddOptionWithLongName("format", OptionParametersCount.Exactly(1))
                .AddValidator(new RegexValidator("^[1-9MmYys]*$"))
                .SetParameterName("FORMAT");

            var parser = new CommandLineParser(settingsBuilder.ToSettings());


            var validationResult = parser.Validate(args);
            if (validationResult.IsValid == false)
            {
                validationResult.WriteErrorsToConsole();
                // i pristup k jednotlivych chybam je mozny:
                Console.WriteLine("Custom errors output: ");
                foreach (var error in validationResult.Errors)
                {
                    Console.WriteLine("---");
                    Console.WriteLine(error.Message);
                    // kazda chyba obsahuje i seznam voleb, kterych se tyka
                    foreach (var optionSettings in error.InvalidOptions)
                    {
                        // ...
                    }
                }
                Console.ReadLine();
                return;
            }


            var result = parser.Parse(args);

            if (result.Options.Contains("help"))
            {
                settingsBuilder.ToSettings().WriteHelpToConsole();
                Console.ReadLine();
                return;
            }

            if (result.Options.Contains("format"))
            {
                string format = result.Options["format"].Value.AsString();
                Console.WriteLine("Format je {0}", format);
            }

            if (result.Options.Contains("numbers"))
            {
                var numbers = new List<int>();
                foreach (var value in result.Options["numbers"].Values)
                {
                    numbers.Add(value.AsInt());
                }
                Console.WriteLine(
                    "Numbers: {0}", 
                    string.Join(", ", numbers.Select(n => n.ToString()).ToArray()));
            }

            Console.ReadLine();
        }
    }
}
